package com.briup.service.impl;

import com.briup.bean.PageBean;
import com.briup.dao.BookDao;
import com.briup.dao.CategoryDao;
import com.briup.dao.extend.BookExtendDao;
import com.briup.exception.ServiceException;
import com.briup.pojo.Book;
import com.briup.pojo.Category;
import com.briup.pojo.extend.BookExtend;
import com.briup.result.ResultCode;
import com.briup.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookExtendDao bookExtendDao;
    @Autowired
    private BookDao bookDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<BookExtend> getBooksByCondition(String keyword, Integer categoryId) {
        List<BookExtend> list = bookExtendDao.selectBooksByCondition(keyword, categoryId);
        return list;
    }

    @Override
    public void addBook(Book book) {
        // 参数校验 验证必填项 验证参数类型等内容
        validate(book);
        // 验证分类信息是否存在
        Integer categoryId = book.getCategoryId();
        if (categoryId != null){
            categoryIsOrNotExist(categoryId);
        }
        // 执行添加的操作
        bookDao.insertBook(book);
    }

    @Override
    public void removeBatchByIds(List<Integer> ids) {
        // 判断书籍信息是否存在
         for (Integer id :
                 ids) {
             bookIsOrNotExist(id);
         }
        // 方式1 新写一个根据数组删除的方法
        bookDao.deleteBookByIds(ids);
        // 方式2 循环调用之前根据id删除的方法
        // for (Integer id :
        //         ids) {
        //     bookDao.deleteBookById(id);
        // }
    }

    @Override
    public PageBean getBooksByConditionAndPage(String keyword, Integer categoryId, Integer pageNum, Integer pageSize) {
        // 计算开始的索引
        Integer pageIndex = (pageNum - 1 ) * pageSize;
        // 查询数据
        List<BookExtend> books = bookExtendDao.selectBooksByConditionAndPage(keyword, categoryId, pageIndex, pageSize);
        // 查询满足条件的书籍总数
        Long total = bookExtendDao.selectCount(keyword, categoryId);
        PageBean pageBean = new PageBean(total,books);
        return pageBean;
    }

    @Override
    public PageInfo getBooksByConditionAndPageNew(String keyword, Integer categoryId,
                                                  Integer pageNum, Integer pageSize) {
        // 1. 开启分页查询 直接传入页码和每页显示的条数
        PageHelper.startPage(pageNum,pageSize);
        // 2. 查询数据 不需要分页查询 使用普通的条件查询即可
        List<BookExtend> bookExtends = bookExtendDao.
                selectBooksByCondition(keyword, categoryId);
        // 3. 封装成PageInfo对象
        PageInfo<BookExtend> pageInfo = new PageInfo<>(bookExtends);
        return pageInfo;
    }

    private void bookIsOrNotExist(Integer id) {
        Book book = bookDao.selectBookById(id);
        if (book == null){
            throw new ServiceException(
                    ResultCode.BOOK_IS_NOT_EXIST
            );
        }
    }

    // 验证分类信息是否存在
    private void categoryIsOrNotExist(Integer categoryId) {
        Category category = categoryDao.selectCategoryById(categoryId);
        if (category == null){
            throw new ServiceException(
                    ResultCode.CATEGORY_IS_NOT_EXIST
            );
        }
    }

    // 参数校验 验证必填项 验证参数类型等内容
    private void validate(Book book) {
        if (!StringUtils.hasText(book.getName())){
            throw new ServiceException(
                    ResultCode.BOOK_NAME_IS_NULL
            );
        }
        if (!StringUtils.hasText(book.getAuthor())){
            throw new ServiceException(
                    ResultCode.BOOK_AUTHOR_IS_NULL
            );
        }
        if (!StringUtils.hasText(book.getPublisher())){
            throw new ServiceException(
                    ResultCode.BOOK_PUBLISHER_IS_NULL
            );
        }
    }


}
